/*
 * LineManager.h
 *
 *  Created on: 2012-9-9
 *      Author: yangentao@gmail.com
 */

#ifndef LINEMANAGER_H_
#define LINEMANAGER_H_
#include "../dep.h"

#include "TextListener.h"

using namespace std;

class lineListener {
public:
	virtual ~lineListener();
	virtual void onLineReplaced(int from, int to, int* newLines, int newLinesCount) = 0;
};

//external should store the line number rather than the position of the line
//line [0, count)
class LineManager: public TextListener {
protected:
	IntVector vec;
public:
	LineManager();
	void dump();
public:
	//how manay lines
	int count();
	//the start position of some line
	int getLineStart(int line);
	//the position belongs which line
	int findLine(int position);
	void onTextReplaced(int from, const String& removeText, const String& insertText);
protected:
	void parseCrLf(const String& text, Vector<int>& lines, int delta);
};

//-----------------inline ---implements------------
//how manay lines
inline int LineManager::count() {
	return vec.size() - 1; //sub the last element
}
//the start position of some line
inline int LineManager::getLineStart(int line) {
	assert(line >=0);
	if (line >= count()) {
		throw IndexOutOfBoundException();
	}
	return vec[line];
}
#endif /* LINEMANAGER_H_ */
